package com.itheima.controller;

import com.itheima.anno.LogOperation;
import com.itheima.pojo.Result;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer pageSize, String name, Integer degree, Integer clazzId) {
        // 修改为调用 clazzService 的 page 方法
        PageResult pageResult = studentService.page(page, pageSize, name, degree, clazzId);
        return Result.success(pageResult);
    }

    //插入学生
    @LogOperation
    @PostMapping
    public Result save(@RequestBody Student student) {
        studentService.save(student);
        return Result.success();
    }

    //删除学生
    @LogOperation
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable Integer[] ids) {
        List<Integer> idList = Arrays.asList(ids);
        studentService.deleteByIds(idList);
        return Result.success();
    }

    //根据id回显学生信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Student student = studentService.getById(id);
        return Result.success(student);
    }

    //根据id修改学生信息
    @LogOperation
    @PutMapping
    public Result update(@RequestBody Student student) {
        studentService.update(student);
        return Result.success();
    }

    //添加学生违纪
    @LogOperation
    @PutMapping("/violation/{id}/{score}")
    public Result addViolation(@PathVariable Integer id, @PathVariable Integer score) {
        studentService.addViolation(id, score);
        return Result.success();
    }

}
